#include "lecturer.h"
#include "Database.h"
#ifndef DATABASE_FILE
#define DATABASE_FILE "Database.Assn2"

void Lecturer::lecturerInterface() {
    Lecturer L;
    while (1) {
        cout << "<<<<<<< Student Attendance System >>>>>>>" << endl;
        cout << "<<<<<<<      Lecturer Interface      >>>>>>>" << endl << endl;

        Database *db;
        db = new Database("Database.Assn2");
        string userid;
        string username;
        string moduleid;
        vector<vector<string> > result = db->query("SELECT * FROM lecturersubjects;");
        for (vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) {
            vector<string> row = *it;
            userid == row.at(0);
            username == row.at(1);
            moduleid == row.at(2);
            cout << " Modules taught: " << "Module ID " << row.at(2) << endl;
            cout << endl;
        }

        int choice = 0;
        cout << "User Management" << endl;
        cout << "   1) Add student to class" << endl;
        cout << "   2) Remove student from class" << endl;
        cout << "Attendance Management" << endl;
        cout << "   3) Post announcement" << endl;
        cout << "   4) View past announcement" << endl;
        cout << "   5) Mark attendance record" << endl;
        cout << "   6) View attendance record" << endl;
        cout << "   7) Print attendance record" << endl;
        cout << "   8) Add Lab/Lecture/Tutorial" << endl;
        cout << "   9) Remove Lab/Lecture/Tutorial" << endl;
        cout << "   10) Exit Program" << endl;
        cout << "   Please enter your choice: " << endl;
        cin >> choice;
        cin.clear();

        if (choice == 1) {
            L.lecturerAddStudent();
        } else if (choice == 2) {
            L.lecturerRemoveStudent();
        } else if (choice == 3) {
            L.postAnnouncement();
        } else if (choice == 4) {
            L.viewPastAnnouncement();
        } else if (choice == 5) {
            L.lecturerMarkAttendance();
        } else if (choice == 6) {
            L.lecturerViewAttendance();
        } else if (choice == 7) {
            L.lecturerAddClass();
        } else if (choice == 8) {
            L.lecturerRemoveClass();
        } else if (choice == 10) {
            cout << "Exiting Program!!!" << endl;
            exit(0);
        }
    }
}

void Lecturer::lecturerAddClass() {
    string newclasstype, newdate, newtime, newmoduleid;
    Lecturer L;
    cout << endl << "Proceeding to create class....." << endl;
    cout << "Please enter module id: ";
    cin >> newmoduleid;
    cin.clear();
    cin.ignore(100, '\n');
    cout << "Please enter class type: ";
    cin >> newclasstype;
    cin.clear();
    cin.ignore(100, '\n');
    cout << "Please enter date: ";
    cin >> newdate;
    cin.clear();
    cin.ignore(100, '\n');
    cout << "Please enter time: ";
    cin >> newtime;
    cin.clear();
    cin.ignore(100, '\n');

    Database *db;
    db = new Database("Database.Assn2");

    string sqlstatement;
    sqlite3 *db1;
    sqlite3_stmt *stmt;

    sqlstatement = "INSERT INTO runningclass (userID, username, moduleID, classID, classType, time, date) VALUES ('" "','" "','" + newmoduleid + "','" "','" + newclasstype + "','" + newtime + "','" + newdate + "')";
    if (sqlite3_open(DATABASE_FILE, &db1) == SQLITE_OK) {
        sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
        sqlite3_step(stmt);
        sqlite3_finalize(stmt);
        sqlite3_close(db1);
        cout << endl;
        cout << "New class for module " << newmoduleid << " is successfully added!" << endl;
        cout << endl;
        return;
    }
    db->close();
    return;

}

void Lecturer::lecturerRemoveClass() {
    Lecturer L;
    Database *db;
    db = new Database("Database.Assn2");
    int choice;
    string removeClass;
    cout << "Enter the class module id that you wish to delete (type -1 to exit): ";
    cin >> removeClass;
    if (removeClass == "-1") {
        L.lecturerInterface();
    } else {
        vector<vector<string> > result = db->query("SELECT * FROM runningclass;");
        for (vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) {
            vector<string> row = *it;
            if (removeClass == row.at(2)) {
                cout << "Module: " << row.at(2) << endl;
                cout << "Enter your choice (1 to delete, -1 to exit): ";
                cin >> choice;
                if (choice == -1) {
                    cout << "Going back to main lecturer interface" << endl;
                    L.lecturerInterface();
                }
                if (choice == 1) {
                    cout << "*********THIS IS IRREVERSIBLE*********" << endl;
                    cout << "1 to confirm delete, -1 to exit" << endl;
                    cout << "Enter your choice: ";
                    cin >> choice;
                    if (choice == -1) {
                        cout << "Going back to main lecturer interface" << endl;
                        L.lecturerInterface();
                    }
                    if (choice == 1) {
                        string sqlstatement1;
                        sqlite3 *db1;
                        sqlite3_stmt *stmt;
                        sqlstatement1 = "DELETE FROM runningclass WHERE moduleID='" + removeClass + "'";

                        if (sqlite3_open(DATABASE_FILE, &db1) == SQLITE_OK) {
                            sqlite3_prepare(db1, sqlstatement1.c_str(), -1, &stmt, NULL);
                            sqlite3_step(stmt);
                            sqlite3_finalize(stmt);
                            sqlite3_close(db1);
                            cout << "Class for module" << removeClass << " has been deleted!" << endl;
                            cout << "Returning to lecturer interface..." << endl;
                            cout << endl;
                            L.lecturerInterface();
                        }

                    }
                }

            }
        }
    }
    db->close();
}

void Lecturer::lecturerAddStudent() {
    string newuserid, newusername, newclassid, newclasstype, newmoduleid;
    Lecturer L;
    cout << endl << "Proceeding to add student into class....." << endl;
    cout << "Please enter student user id: ";
    cin >> newuserid;
    cin.clear();
    cin.ignore(100, '\n');
    cout << "Please enter student username: ";
    cin >> newusername;
    cin.clear();
    cin.ignore(100, '\n');
    cout << "Please enter class id: ";
    cin >> newclassid;
    cin.clear();
    cin.ignore(100, '\n');
    cout << "Please enter class type: ";
    cin >> newclasstype;
    cin.clear();
    cin.ignore(100, '\n');
    cout << "Please enter module id: ";
    cin >> newmoduleid;
    cin.clear();
    cin.ignore(100, '\n');


    Database *db;
    db = new Database("Database.Assn2");

    string sqlstatement;
    sqlite3 *db1;
    sqlite3_stmt *stmt;

    sqlstatement = "INSERT INTO studentsubject (userID, username, classID, classType, moduleID) VALUES ('" + newuserid + "','" + newusername + "','" + newclassid + "','" + newclasstype + "','" + newmoduleid + "')";
    if (sqlite3_open(DATABASE_FILE, &db1) == SQLITE_OK) {
        sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
        sqlite3_step(stmt);
        sqlite3_finalize(stmt);
        sqlite3_close(db1);
        cout << endl;
        cout << "Student is successfully added into class" << newclassid << endl;
        cout << endl;
        return;
    }
    db->close();
    return;
}

void Lecturer::lecturerRemoveStudent() {
    Lecturer L;
    Database *db;
    db = new Database("Database.Assn2");
    int choice;
    string removeStud;
    cout << "Enter the student user id that you wish to remove (type -1 to exit): ";
    cin >> removeStud;
    if (removeStud == "-1") {
        L.lecturerInterface();
    } else {
        vector<vector<string> > result = db->query("SELECT * FROM studentsubject;");
        for (vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) {
            vector<string> row = *it;
            if (removeStud == row.at(0)) {
                cout << "Student user id: " << row.at(0) << endl;
                cout << "Enter your choice (1 to delete, -1 to exit): ";
                cin >> choice;
                if (choice == -1) {
                    cout << "Going back to main lecturer interface" << endl;
                    L.lecturerInterface();
                }
                if (choice == 1) {
                    cout << "*********THIS IS IRREVERSIBLE*********" << endl;
                    cout << "1 to confirm delete, -1 to exit" << endl;
                    cout << "Enter your choice: ";
                    cin >> choice;
                    if (choice == -1) {
                        cout << "Going back to main lecturer interface" << endl;
                        L.lecturerInterface();
                    }
                    if (choice == 1) {
                        string sqlstatement1;
                        sqlite3 *db1;
                        sqlite3_stmt *stmt;
                        sqlstatement1 = "DELETE FROM studentsubject WHERE userID='" + removeStud + "'";

                        if (sqlite3_open(DATABASE_FILE, &db1) == SQLITE_OK) {
                            sqlite3_prepare(db1, sqlstatement1.c_str(), -1, &stmt, NULL);
                            sqlite3_step(stmt);
                            sqlite3_finalize(stmt);
                            sqlite3_close(db1);
                            cout << "Student user id " << removeStud << " is removed from class!" << endl;
                            cout << "Returning to lecturer interface..." << endl;
                            cout << endl;
                            L.lecturerInterface();
                        }

                    }
                }

            }
        }
    }
    db->close();
}

void Lecturer::lecturerMarkAttendance() {

    Database *db;
    db = new Database("Database.Assn2");

    string userid;
    string username;
    string classid;
    string classtype;
    string moduleid;

    vector<vector<string> > result = db->query("SELECT * FROM studentsubject;");
    for (vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) {
        vector<string> row = *it;
        userid == row.at(0);
        username == row.at(1);
        classid == row.at(2);
        classtype == row.at(3);
        moduleid == row.at(4);
        cout << " Student ID " << row.at(0) << " Student UserName " << row.at(1) << " Class ID " << row.at(2) << endl;
        cout << endl;
    }
    string newmoduleid, newuserid, newstatus, newdate, newduration, newmodulegrp;
    Lecturer L;
    cout << endl << "Proceeding to add student into class....." << endl;
    cout << "Please enter Module ID: ";
    cin >> newmoduleid;
    cout << "Please enter student user ID: ";
    cin >> newuserid;
    cout << "Please enter status (P/A): ";
    cin >> newstatus;
    cout << "Please enter date: ";
    cin >> newdate;
    cout << "Please enter duration: ";
    cin >> newduration;
    cout << "Please enter module group: ";
    cin >> newmodulegrp;

    string sqlstatement;
    sqlite3 *db1;
    sqlite3_stmt *stmt;

    sqlstatement = "INSERT INTO attendance (moduleID, userID, status, date, duration, modulegrp) VALUES ('" + newmoduleid + "','" + newuserid + "','" + newstatus + "','" + newdate + "','" + newduration + "','" + newmodulegrp + "')";
    if (sqlite3_open(DATABASE_FILE, &db1) == SQLITE_OK) {
        sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
        sqlite3_step(stmt);
        sqlite3_finalize(stmt);
        sqlite3_close(db1);
        cout << endl;
        cout << "Attendance Marked" << endl;
        cout << endl;
        return;
    }

    db->close();
    return;
}

void Lecturer::lecturerViewAttendance() {
    Database *db;
    db = new Database("Database.Assn2");

    string moduleid;
    string userid;
    string classid;
    string status;
    string date;
    string duration;
    string modulegrp;

    vector<vector<string> > result = db->query("SELECT * FROM attendance");
    for (vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) {
        vector<string> row = *it;
        moduleid == row.at(0);
        userid == row.at(1);
        status == row.at(2);
        date == row.at(3);
        duration == row.at(4);
        modulegrp == row.at(5);
        cout << " Module ID: " << row.at(0) << " User ID: " << row.at(1) << " Status: " << row.at(2) << " Date: " << row.at(3) << " Duration: " << row.at(4) << " Module Group: " << row.at(5) << endl;
        cout << endl;
    }
}

void Lecturer::postAnnouncement() {
    string newmessage;
    cout << "Please enter your announcement: ";
    cin >> newmessage;

    Database *db;
    db = new Database("Database.Assn2");

    string sqlstatement;
    sqlite3 *db1;
    sqlite3_stmt *stmt;
    cout << newmessage << endl;

    sqlstatement = "INSERT INTO announcement (message) VALUES ('" + newmessage + "')";
    if (sqlite3_open(DATABASE_FILE, &db1) == SQLITE_OK) {
        sqlite3_prepare(db1, sqlstatement.c_str(), -1, &stmt, NULL);
        sqlite3_step(stmt);
        sqlite3_finalize(stmt);
        sqlite3_close(db1);
        cout << "Announcement added" << endl;
        cout <<endl;
        return;
    }

    db->close();
    return;
}

void Lecturer::viewPastAnnouncement() {
    Database *db;
    db = new Database("Database.Assn2");
    
    string aid;
    string message;

    vector<vector<string> > result = db->query("SELECT * FROM announcement");
    for (vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) {
        vector<string> row = *it;
        aid == row.at(0);
        message == row.at(1);
        cout << " Announcement ID: " << row.at(0) << " Announcement: " << row.at(1) << endl;
        cout << endl;
    }
}

#endif
